package org.kongqian.basic.controller.impl;

import org.kongqian.basic.controller.IFastDfsService;
import org.kongqian.basic.utils.FastdfsUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class FastDfsServiceImpl implements IFastDfsService {

    @Value("${fastdfs.url}")
    private String fastdfsurl;

    @Override
    public String upload(MultipartFile file) {
        //获取文件名 file.getOriginalFilename()
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.indexOf(".") + 1);
        String path=null;
        try {
            //file.getBytes() 可以理解文件是存到Bytes中的  需要的参数为文件和扩展名
            path = FastdfsUtil.upload(file.getBytes(), extName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

    @Override
    public void deleteFasdfs(String path) { //删除需要的参数为组名和文件名
        path = path.substring(fastdfsurl.length()+1);
        int i = path.indexOf("/");
        String group = path.substring(0, i);
        String filePath = path.substring(i + 1);
        FastdfsUtil.delete(group,filePath);
    }
}
